package com.zxh.springboot.bean;

import org.springframework.util.ReflectionUtils;
import org.springframework.web.WebApplicationInitializer;

import javax.servlet.ServletContainerInitializer;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.HandlesTypes;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Modifier;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;

public class MySpringServletContainerInitializer implements ServletContainerInitializer {

    public void onStartup(Set<Class<?>> set, ServletContext ctx) throws ServletException {
        List<WebApplicationInitializer> initializerList = new LinkedList<WebApplicationInitializer>();

        if(set != null){
            for (final Class<?> aClass : set) {
                if(!aClass.isInterface() && !Modifier.isAbstract(aClass.getModifiers()) && WebApplicationInitializer.class.isAssignableFrom(aClass)){
                    try {
                        initializerList.add((WebApplicationInitializer) ReflectionUtils.accessibleConstructor(aClass).newInstance());
                    } catch (IllegalAccessException e) {
                        e.printStackTrace();
                    } catch (InstantiationException e) {
                        e.printStackTrace();
                    } catch (NoSuchMethodException e) {
                        e.printStackTrace();
                    } catch (InvocationTargetException e) {
                        e.printStackTrace();
                    }
                }
            }
        }

        if(initializerList.isEmpty()){

        }
    }
}
